﻿package {
	
import pete.layout.Distribute;
import pete.tools.Random;
import flash.display.BlendMode;
import flash.display.DisplayObject;
import flash.display.DisplayObjectContainer;
import flash.display.Shape;
import flash.display.Sprite;
import flash.display.StageAlign;
import flash.display.StageScaleMode;
import flash.events.Event;
import flash.events.MouseEvent;
import flash.net.navigateToURL;
import flash.net.URLRequest;
import flash.text.TextField;
import flash.text.TextFormat;



public class ColorBar extends Sprite {
	
	// Properties
	// -----------------------------------------------------------
	
	public static const COLOR_HEIGHT:int 	= 50;
	public static const BOX_TOP:int		= 2;
	public static const BOX_HEIGHT:int 	= 23;
	public static const TITLE_LEFT:int		= 275;
	
	private var bgLayer:Sprite;
	private var textLayer:Sprite;
	private var bars:Sprite;
	private var tabs:Array;
	private var tabMasks:Sprite;
	private var white:Shape;
	private var textFormat:TextFormat;
	private var header:ScreenText;
	
	
	
	// Constructor
	// -----------------------------------------------------------
	
	public function ColorBar() {
		
		build();
		
		addEventListener(Event.ADDED_TO_STAGE, handleStage);
	}
	
	
	
	// Public Functions
	// -----------------------------------------------------------
	
	public function setTitle($value:String):void {
		
		header.text = $value.toUpperCase();
		white.width = header.width + 8;
	}
	
	public function setTab($index:int, $menuItem:Object):void {
		
		var tab:ScreenText		= tabs[$index];
		tab.text			= $menuItem.title.toUpperCase();
		
		if ($menuItem.path == "<null>") {
			tab.buttonMode	= false;
		}
		else {
			tab.name		= $menuItem.path;
			tab.addEventListener	(MouseEvent.MOUSE_OVER, handleMouseOver);
			tab.addEventListener	(MouseEvent.CLICK, handleTabClick);
		}
	}
	
	public function setTexture($texture:Sprite):void {
		
		$texture.y			= 25;
		$texture.blendMode 		= BlendMode.MULTIPLY;
		$texture.mouseEnabled	= false;
		
		addChildAt			($texture, 1);
	}
	
	
	
	// Private Functions
	// -----------------------------------------------------------
	
	private function build():void {
		
		bgLayer 	= new Sprite();
		textLayer	= new Sprite();
		tabs		= new Array();
		
		drawText();
		drawBars();
		drawBox();
		drawSlivers();
		
		addChildAt	(bgLayer, 0);
		addChildAt	(textLayer, 1);
	}
	
	private function drawBars():void {
		
		bars = new Sprite();
		tabMasks = new Sprite();
		
		// drawBar(0xcccccc); // Grey
		drawBar(0xffff00, 0x00ffff); // Yellow
		drawBar(0x00ffff, 0xffff00); // Cyan
		drawBar(0x00ff00, 0x00ffff); // Green
		drawBar(0xff00ff, 0xffff00); // Magenta
		drawBar(0xff0000, 0x00ffff); // Red
		drawBar(0x0000ff, 0xffff00); // Blue
		
		Distribute.horizontalIn(bars);
		Distribute.horizontalIn(tabMasks);
		
		bgLayer.addChild(bars);
		bgLayer.addChild(tabMasks);
	}
	 
	private function drawBar(bc:uint, tc:uint):void {
		
		var bar:Shape 		= new Shape();
		bar.graphics.beginFill	(bc);
		bar.graphics.drawRect	(0, 0, 60, COLOR_HEIGHT);
		
		var tabMask:Sprite		= new Sprite();
		tabMask.mouseEnabled		= false;
		tabMask.graphics.beginFill	(tc);
		tabMask.graphics.drawRect	(0, 0, 60, COLOR_HEIGHT);
		
		var tab:ScreenText		= new ScreenText(textFormat);
		tab.colors 			= [tc];
		tab.y				= 27;
		tab.buttonMode		= true;
		tab.scaleX = tab.scaleY	= 2/3;
		tab.mask			= tabMask;
		
		bars.addChild			(bar);
		tabMasks.addChild		(tabMask);
		textLayer.addChild		(tab);
		tabs.push			(tab);
		
	}
	
	private function drawBox():void {
		
		var box:Shape 		= new Shape();
		box.graphics.beginFill	(0x1a1a1a);
		box.graphics.drawRect	(0, 0, 360, BOX_HEIGHT);
		box.y 				= BOX_TOP;
		
		bgLayer.addChild		(box);
	}
	
	private function drawSlivers():void {
		
		var slivers:Sprite 		= new Sprite();
		slivers.x 			= 240;
		slivers.y 			= BOX_TOP;
		
		var d:Shape 			= drawSliver(0x000000); // Dark
		var m:Shape 			= drawSliver(0x1a1a1a); // Medium
		var l:Shape 			= drawSliver(0x303030); // Light
		
		slivers.addChild		(d);
		slivers.addChild		(m);
		slivers.addChild		(l);
		
		Distribute.horizontalIn	(slivers);
		
		bgLayer.addChild		(slivers);
		
	}
	
	private function drawSliver(c:uint):Shape {
		
		var sliver:Shape 		= new Shape();
		sliver.graphics.beginFill	(c);
		sliver.graphics.drawRect	(0, 0, 20, BOX_HEIGHT);
		
		return 			(sliver);
	}
	
	private function drawText():void {
		
		white				= new Shape();
		white.graphics.beginFill	(0xffffff);
		white.graphics.drawRect	(0, 0, 50, BOX_HEIGHT);
		white.x			= TITLE_LEFT;
		white.y			= BOX_TOP;
		
		textFormat			= new TextFormat();
		textFormat.font 		= new SG03().fontName;
		textFormat.size 		= 8;
		textFormat.letterSpacing 	= 1;
		
		header 			= new ScreenText(textFormat);
		header.colors 		= [0x00ffff]; // 0xffff00, 0xff00ff, 0x00ffff, 0xff0000, 0x00ff00, 0x0000ff];
		header.x			= white.x + 5.5
		header.y			= -3;
		header.buttonMode		= true;
		header.addEventListener	(MouseEvent.MOUSE_OVER, handleMouseOver);
		header.addEventListener	(MouseEvent.CLICK, handleHeaderClick);
		
		textLayer.addChild		(white);
		textLayer.addChild		(header);
	}
	
	
	
	// Private Functions
	// -----------------------------------------------------------
	
	private function handleStage(e:Event):void {
		
		stage.scaleMode 		= StageScaleMode.NO_SCALE;
		stage.align 			= StageAlign.TOP_LEFT;
		stage.addEventListener	(Event.RESIZE, resizeDisplay);
		
		resizeDisplay();
	}
	
	private function resizeDisplay(e:Event = null):void {
		
		bgLayer.width 			= stage.stageWidth;
		
		var index:int				= 0;
		
		for (var i:String in tabs) {
			
			var tab:ScreenText		= tabs[i];
			tab.x 				= Math.round((stage.stageWidth / bars.numChildren) * index) + 4;
			
			switch (index % 2) {
				
				case 0:
				tab.x += .4;
				break;
				
				case 1:
				tab.x += .4;
				break;
				
				default:
				break;
			}
			
			index++;
		}
		white.x		= (stage.stageWidth / 4) + 20;
		white.width 		= header.width + 8;
		header.x		= white.x + 5.5;
	}
	
	private function handleMouseOver(me:MouseEvent):void {
		
		me.target.removeEventListener(MouseEvent.MOUSE_OVER, handleMouseOver);
		me.target.addEventListener(MouseEvent.MOUSE_OUT, handleMouseOut);
		
		var d:int = Random.choose(2, 6);
		
		ColorError.getInstance().split(me.target, .075, d, "RB");
	}
	
	private function handleMouseOut(me:MouseEvent):void {
		
		me.target.addEventListener(MouseEvent.MOUSE_OVER, handleMouseOver);
		me.target.removeEventListener(MouseEvent.MOUSE_OUT, handleMouseOut);
		
		ColorError.getInstance().join(me.target, .15);
	}
	
	private function handleHeaderClick(me:MouseEvent):void {
		
		navigateToURL( new URLRequest("/"), "_self" );
	}
	
	private function handleTabClick(me:MouseEvent):void {
		
		var path:String = "/";
		
		if (me.target.parent.name != "<front>") path += me.target.parent.name;
		
		navigateToURL( new URLRequest(path), "_self" );
	}
	
	
}
}
